package pl.register.android.activities.profile.ui;

import android.content.Context;
import android.view.View;
import android.widget.*;
import com.actionbarsherlock.internal.widget.IcsLinearLayout;
import pl.register.android.R;
import pl.register.android.activities.profile.logic.StudentProfilePresenter;
import pl.register.android.homework.Homework;
import pl.register.android.homework.HomeworkTopic;
import pl.register.android.lesson.Lesson;
import pl.register.android.lesson.TagLesson;
import pl.register.android.widget.button.RatingButton;
import pl.register.android.widget.table.StudentLessonsTableAdapter;
import pl.register.android.widget.textview.VerticalTextView;

import java.util.List;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-20
 */
public class StudentHomeworksTableAdapter extends StudentLessonsTableAdapter implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private StudentProfileView studentProfileView;

    private StudentProfilePresenter studentProfilePresenter;

    public StudentHomeworksTableAdapter(Context context, StudentProfileView studentProfileView, StudentProfilePresenter studentProfilePresenter) {
        super(context);
        this.context = context;
        this.studentProfileView = studentProfileView;
        this.studentProfilePresenter = studentProfilePresenter;
    }

    @Override
    public View getLessonSectionView(View convertView, int tagPosition) {
        ViewSectionHolder holder;
        if (convertView == null) {
            holder = new ViewSectionHolder();
            convertView = inflater.inflate(R.layout.table_section_lessons_with_tag, null);
            holder.llHomeworks = (LinearLayout) convertView.findViewById(R.id.lesson_columns_container);
            holder.tvTagName = (TextView) convertView.findViewById(R.id.tag_name_text_view);
            convertView.setTag(holder);
        } else {
            holder = (ViewSectionHolder) convertView.getTag();
        }
        fillSection(holder, tagPosition);
        return convertView;
    }

    private void fillSection(ViewSectionHolder holder, int tagPosition) {
        TagLesson tag = getTag(tagPosition);
        List<Lesson> lessons = getLessonsBy(tag);

        if (lessons != null && !lessons.isEmpty()) {
            for (int index = 0; index < lessons.size(); index++) {
                boolean itsLastColumn = (index == lessons.size() - 1);
                Lesson lesson = lessons.get(index);
                View columnView = createHomeworkColumn(itsLastColumn);
                ViewColumnHolder columnHolder = (ViewColumnHolder) columnView.getTag();
                fillColumn(columnHolder, lesson, lesson.getHomework());
                holder.llHomeworks.addView(columnView);
            }
            holder.tvTagName.setText(tag.getName());
        } else {
            holder.tvTagName.setVisibility(View.GONE);
        }

    }

    private View createHomeworkColumn(boolean itsLastColumn) {
        ViewColumnHolder columnHolder = new ViewColumnHolder();
        View columnClasses = inflater.inflate(R.layout.table_column_lesson, null);
        columnHolder.vtvTopic = (VerticalTextView) columnClasses.findViewById(R.id.topic_vertical_text_view);
        columnHolder.tvSchedule = ((TextView) columnClasses.findViewById(R.id.schedule_text_view));
        columnHolder.cbIsDone = (CheckBox) columnClasses.findViewById(R.id.lesson_check_box);
        columnHolder.btnRating = (RatingButton) columnClasses.findViewById(R.id.rating_button);
        columnClasses.setTag(columnHolder);

        if (itsLastColumn) {
            IcsLinearLayout llTopics = (IcsLinearLayout) columnClasses.findViewById(R.id.topics_linear_layout);
            llTopics.setShowDividers(IcsLinearLayout.SHOW_DIVIDER_NONE);
            IcsLinearLayout llSchedule = (IcsLinearLayout) columnClasses.findViewById(R.id.schedule_linear_layout);
            llSchedule.setShowDividers(IcsLinearLayout.SHOW_DIVIDER_NONE);
        }

        return columnClasses;
    }

    private void fillColumn(ViewColumnHolder holder, Lesson lesson, Homework homework) {
        HomeworkTopic homeworkTopic = homework.getTopic();
        VerticalTextView vtvTopic = holder.vtvTopic;
        vtvTopic.setText(homeworkTopic.getTopic());

        TextView tvSchedule = holder.tvSchedule;
        tvSchedule.setText(lesson.getHomeworkDateString());

        CheckBox cbPresence = holder.cbIsDone;
        cbPresence.setChecked(homework.isDone());
        cbPresence.setTag(homework);
        cbPresence.setOnCheckedChangeListener(this);

        RatingButton btnRating = holder.btnRating;
        btnRating.setRating(homework.getRating());
        btnRating.setTag(homework);
        btnRating.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rating_button:
                RatingButton btnRation = (RatingButton) view;
                Homework homework = (Homework) view.getTag();
                homework.setRating(btnRation.getRating());
                studentProfilePresenter.saveHomework(homework);
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        switch (compoundButton.getId()) {
            case R.id.lesson_check_box:
                Homework homework = (Homework) compoundButton.getTag();
                homework.setDone(isChecked);
                studentProfilePresenter.saveHomework(homework);
                break;
        }
    }

    public static class ViewSectionHolder {
        public TextView tvTagName;
        public LinearLayout llHomeworks;
    }

    public static class ViewColumnHolder {
        public VerticalTextView vtvTopic;
        public TextView tvSchedule;
        public CheckBox cbIsDone;
        public RatingButton btnRating;
    }

}